<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="二级分类" prop="secondaryClassification">
                <el-input v-model="queryParams.secondaryClassification" placeholder="请输入二级分类" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="物品名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入物品名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="销售价格" prop="price">
                <el-input v-model="queryParams.price" placeholder="请输入销售价格" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="成本价格" prop="costPrice">
                <el-input v-model="queryParams.costPrice" placeholder="请输入成本价格" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="元气" prop="yuanqi">
                <el-input v-model="queryParams.yuanqi" placeholder="请输入元气" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="利润" prop="profitPrice">
                <el-input v-model="queryParams.profitPrice" placeholder="请输入利润" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="元气利润比" prop="yuanqiProportion">
                <el-input v-model="queryParams.yuanqiProportion" placeholder="请输入元气利润比" clearable @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <el-form-item label="锻造等级" prop="levels">
                <el-input v-model="queryParams.levels" placeholder="请输入锻造等级" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="解锁条件" prop="unlockConditions">
                <el-input v-model="queryParams.unlockConditions" placeholder="请输入解锁条件" clearable @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <!-- <el-form-item label="装备品质" prop="quality">
                <el-input v-model="queryParams.quality" placeholder="请输入装备品质" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="物品效果" prop="itemEffect">
                <el-input v-model="queryParams.itemEffect" placeholder="请输入物品效果" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="材料图片" prop="photo">
                <el-input v-model="queryParams.photo" placeholder="请输入材料图片" clearable @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <el-form-item label="合成方式" prop="synthesisMethod">
                <el-select clearable v-model="queryParams.synthesisMethod">
                    <el-option label="基础材料" value="0">基础材料</el-option>
                    <el-option label="副本活得" value="1">副本活得</el-option>
                    <el-option label="可合成" value="2">可合成</el-option>
                    <el-option label="声望值" value="3">声望值</el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:items:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:items:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['system:items:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:items:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="itemsList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column label="ID" align="center" prop="id" /> -->
            <el-table-column label="二级分类" align="center" prop="secondaryClassification" />
            <el-table-column label="物品名称" align="center" prop="name" />
            <el-table-column label="销售价格" align="center" prop="price" />
            <el-table-column label="成本价格" align="center" prop="costPrice" />
            <el-table-column label="元气" align="center" prop="yuanqi" />
            <el-table-column label="利润" align="center" prop="profitPrice" />
            <el-table-column label="元气利润比" align="center" prop="yuanqiProportion" />
            <el-table-column label="生产类型" align="center" prop="productionType">
                <template slot-scope="scope">
                    <span v-if="scope.row.synthesisMethod==1">锻造</span>
                    <span v-if="scope.row.synthesisMethod==2">裁缝</span>
                    <span v-if="scope.row.synthesisMethod==3">炼药</span>
                    <span v-if="scope.row.synthesisMethod==4">雕刻</span>
                    <span v-if="scope.row.synthesisMethod==5">烹饪</span>
                    <span v-if="scope.row.synthesisMethod==6">挖矿</span>
                    <span v-if="scope.row.synthesisMethod==7">采集</span>
                    <span v-if="scope.row.synthesisMethod==8">钓鱼</span>
                </template>
            </el-table-column>
            <el-table-column label="锻造等级" align="center" prop="levels" />
            <el-table-column label="解锁条件" align="center" prop="unlockConditions" />
            <el-table-column label="装备品质" align="center" prop="quality" />
            <el-table-column label="物品效果" align="center" prop="itemEffect" />
            <el-table-column label="材料图片" align="center" prop="photo" >
                <template slot-scope="scope">
                <img v-if="scope.row.photo" :src="scope.row.photo" alt="" style="width:50px;height:50px;">
                </template>
            </el-table-column>
            <el-table-column label="合成方式" align="center" prop="synthesisMethod">
                <template slot-scope="scope">
                    <span v-if="scope.row.synthesisMethod==0">基础材料</span>
                    <span v-if="scope.row.synthesisMethod==1">副本活得</span>
                    <span v-if="scope.row.synthesisMethod==2">可合成</span>
                    <span v-if="scope.row.synthesisMethod==3">声望值</span>
                </template>
            </el-table-column>
            <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:items:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:items:remove']">删除</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-if="scope.row.synthesisMethod==2">合成物品配置</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改交易行数据对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="二级分类" prop="secondaryClassification">
                    <el-input v-model="form.secondaryClassification" placeholder="请输入二级分类" />
                </el-form-item>
                <el-form-item label="物品名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入物品名称" />
                </el-form-item>
                <el-form-item label="销售价格" prop="price">
                    <el-input v-model="form.price" placeholder="请输入销售价格" />
                </el-form-item>
                <el-form-item label="成本价格" prop="costPrice">
                    <el-input v-model="form.costPrice" placeholder="请输入成本价格" />
                </el-form-item>
                <el-form-item label="元气" prop="yuanqi">
                    <el-input v-model="form.yuanqi" placeholder="请输入元气" />
                </el-form-item>
                <el-form-item label="利润" prop="profitPrice">
                    <el-input v-model="form.profitPrice" placeholder="请输入利润" />
                </el-form-item>
                <el-form-item label="元气利润比" prop="yuanqiProportion">
                    <el-input v-model="form.yuanqiProportion" placeholder="请输入元气利润比" />
                </el-form-item>
                <el-form-item label="生产类型" prop="synthesisMethod">
                    <el-select clearable v-model="form.synthesisMethod">
                        <el-option label="锻造" value="1">锻造</el-option>
                        <el-option label="裁缝" value="2">裁缝</el-option>
                        <el-option label="炼药" value="3">炼药</el-option>
                        <el-option label="雕刻" value="4">雕刻</el-option>
                        <el-option label="烹饪" value="5">烹饪</el-option>
                        <el-option label="挖矿" value="6">挖矿</el-option>
                        <el-option label="采集" value="7">采集</el-option>
                        <el-option label="钓鱼" value="8">钓鱼</el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="锻造等级" prop="levels">
                    <el-input v-model="form.levels" placeholder="请输入锻造等级" />
                </el-form-item>
                <el-form-item label="解锁条件" prop="unlockConditions">
                    <el-input v-model="form.unlockConditions" placeholder="请输入解锁条件" />
                </el-form-item>
                <el-form-item label="装备品质" prop="quality">
                    <el-input v-model="form.quality" placeholder="请输入装备品质" />
                </el-form-item>
                <el-form-item label="物品效果" prop="itemEffect">
                    <el-input v-model="form.itemEffect" placeholder="请输入物品效果" />
                </el-form-item>
                <el-form-item label="材料图片" prop="photo">
                    <!-- <image-upload v-model="form.photo" /> -->
                    
                    <el-upload ref="upload" list-type="picture-card" :file-list="fileList" :limit="fileLimitNum"
                                :action="baseUrl+ '/system/base/upload'" :on-remove="handleRemove" :on-success="handleFileSuccess"
                                :on-error="handleFileError" :on-exceed="handleExceed" :before-upload='beforeUpload'>
                                <i class="el-icon-plus"></i>
                                <div class="el-upload__tip text-center" slot="tip"></div>
                            </el-upload>
                </el-form-item>
                <el-form-item label="合成方式" prop="synthesisMethod">
                    <el-select clearable v-model="form.synthesisMethod">
                        <el-option label="基础材料" value="0">基础材料</el-option>
                        <el-option label="副本活得" value="1">副本活得</el-option>
                        <el-option label="可合成" value="2">可合成</el-option>
                        <el-option label="声望值" value="3">声望值</el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="删除标志" prop="delFlag">
                    <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
  <script>
import { listItems, getItems, delItems, addItems, updateItems } from "@/api/system/tradingHouse";

export default {
    name: "Items",
    data() {
        return {
            baseUrl: process.env.VUE_APP_BASE_API,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 交易行数据表格数据
            itemsList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                secondaryClassification: null,
                name: null,
                price: null,
                costPrice: null,
                yuanqi: null,
                profitPrice: null,
                yuanqiProportion: null,
                productionType: null,
                levels: null,
                unlockConditions: null,
                quality: null,
                itemEffect: null,
                photo: null,
                synthesisMethod: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                secondaryClassification: [
                    { required: true, message: "二级分类不能为空", trigger: "blur" }
                ],
                name: [
                    { required: true, message: "物品名称不能为空", trigger: "blur" }
                ],
                price: [
                    { required: true, message: "销售价格不能为空", trigger: "blur" }
                ],
                costPrice: [
                    { required: true, message: "成本价格不能为空", trigger: "blur" }
                ],
                yuanqi: [
                    { required: true, message: "元气不能为空", trigger: "blur" }
                ],
                profitPrice: [
                    { required: true, message: "利润不能为空", trigger: "blur" }
                ],
                yuanqiProportion: [
                    { required: true, message: "元气利润比不能为空", trigger: "blur" }
                ],
                productionType: [
                    { required: true, message: "生产类型不能为空", trigger: "change" }
                ],
                levels: [
                    { required: true, message: "锻造等级不能为空", trigger: "blur" }
                ],
                unlockConditions: [
                    { required: true, message: "解锁条件不能为空", trigger: "blur" }
                ],
                quality: [
                    { required: true, message: "装备品质不能为空", trigger: "blur" }
                ],
                itemEffect: [
                    { required: true, message: "物品效果不能为空", trigger: "blur" }
                ],
                photo: [
                    { required: true, message: "材料图片不能为空", trigger: "blur" }
                ],
                synthesisMethod: [
                    { required: true, message: "合成方式不能为空", trigger: "blur" }
                ],
            },
            fileLimitNum: 1,
            fileList: []
        };
    },
    created() {
        this.getList();
    },
    methods: {
        handleFileError(response, file, fileList) {
            console.log(response, file, fileList)
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            console.log(response, file, fileList)
            if (response.code == 200) {
                this.fileList = fileList
            }
            else {
                this.fileList.splice(this.fileList.length - 1, 1)
                this.$message.error(response.msg);

            }
        },
        //文件移除操作
        handleRemove(file, fileList) {
            this.fileList = fileList//或者data.fileList = []; //清空fileList所有数组    
        },
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 ${this.fileLimitNum} 个文件`);
        },
        beforeUpload(file) {
            console.log(file.type)
            let typeList = ['image/jpeg', 'image/png']
            const isLT2M = file.size / 1024 / 1024 < 2;
            if (!typeList.includes(file.type)) {
                this.$message.error('上传图片只能是 JPG/PNG 格式!');
            }
            if (!isLT2M) {
                this.$message.error('上传图片大小不能超过 2MB!');
            }
            return typeList.includes(file.type) && isLT2M;
        },
        onSuccess(response, file, fileList) {
            this.form.avatar = URL.createObjectURL(file.raw);
        },
        /** 查询交易行数据列表 */
        getList() {
            this.loading = true;
            listItems(this.queryParams).then(response => {
                this.itemsList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                secondaryClassification: null,
                name: null,
                price: null,
                costPrice: null,
                yuanqi: null,
                profitPrice: null,
                yuanqiProportion: null,
                productionType: null,
                levels: null,
                unlockConditions: null,
                quality: null,
                itemEffect: null,
                photo: null,
                synthesisMethod: null,
                delFlag: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            };
            this.fileList = []
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加交易行数据";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getItems(id).then(response => {
                this.form = response.data;
                if (response.data.photo) {
                    for (var i in response.data.photo.split(',')) {
                        let url = response.data.photo.split(',')[i]
                        this.fileList.push({
                            name: url.split('/')[url.split('/').length - 1],
                            url: url
                        })
                    }
                }
                this.open = true;
                this.title = "修改交易行数据";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    let params = JSON.parse(JSON.stringify(this.form))

                    let photo = []
                    for (var i in this.fileList) {
                        if (this.fileList[i].response) {
                            photo.push(this.fileList[i].response.data)
                        }
                        else {
                            photo.push(this.fileList[i].url)
                        }
                    }
                    params.photo = photo.join()
                    if (this.form.id != null) {
                        updateItems(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.fileList = []
                            this.getList();
                        });
                    } else {
                        addItems(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.fileList = []
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除交易行数据编号为"' + ids + '"的数据项？').then(function () {
                return delItems(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/items/export', {
                ...this.queryParams
            }, `items_${new Date().getTime()}.xlsx`)
        }
    }
};
  </script>
  